package com.ruoyi.web.controller.productionre;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.production.domain.SimpleProduction;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.production.domain.SeedReturn;
import com.ruoyi.production.service.ISeedReturnService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 生产资料退回Controller
 *
 * @author ruoyi
 * @date 2021-07-11
 */
@Api("退回操作")
@RestController
@RequestMapping("/production/seedre")
public class SeedReturnController extends BaseController
{
    @Autowired
    private ISeedReturnService seedReturnService;

    /**
     * 查询生产资料退回列表
     */
    @ApiOperation("查询生产资料退回列表")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('production:seedre:list')")
    @GetMapping("/list")
    public TableDataInfo list(SeedReturn seedReturn)
    {
        startPage();
        List<SeedReturn> list = seedReturnService.selectSeedReturnList(seedReturn);
        return getDataTable(list);
    }

    /**
     * 导出生产资料退回列表
     */
    @ApiOperation("导出生产资料退回列表")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('production:seedre:export')")
    @Log(title = "种子退回", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SeedReturn seedReturn)
    {
        List<SeedReturn> list = seedReturnService.selectSeedReturnList(seedReturn);
        ExcelUtil<SeedReturn> util = new ExcelUtil<SeedReturn>(SeedReturn.class);
        return util.exportExcel(list, "种子退回数据");
    }

    /**
     * 获取生产资料退回详细信息
     */
    @ApiOperation("获取生产资料退回详细信息")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('production:seedre:query')")
    @GetMapping(value = "/{seedReturnId}")
    public AjaxResult getInfo(@PathVariable("seedReturnId") Long seedReturnId)
    {
        return AjaxResult.success(seedReturnService.selectSeedReturnById(seedReturnId));
    }

    /**
     * 新增生产资料退回
     */
    @ApiOperation("新增生产资料退回")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('production:seedre:add')")
    @Log(title = "种子退回", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SeedReturn seedReturn)
    {
        return toAjax(seedReturnService.insertSeedReturn(seedReturn));
    }

    /**
     * 修改生产资料退回
     */
    @ApiOperation("修改生产资料退回")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('production:seedre:edit')")
    @Log(title = "种子退回", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SeedReturn seedReturn)
    {
        return toAjax(seedReturnService.updateSeedReturn(seedReturn));
    }

    /**
     * 删除生产资料退回
     */
    @ApiOperation("删除生产资料退回")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('production:seedre:remove')")
    @Log(title = "种子退回", businessType = BusinessType.DELETE)
	@DeleteMapping("/{seedReturnIds}")
    public AjaxResult remove(@PathVariable Long[] seedReturnIds)
    {
        return toAjax(seedReturnService.deleteSeedReturnByIds(seedReturnIds));
    }

    /**
     * 获取蔬菜类型及名称下拉框
     */
    @ApiOperation("获取蔬菜类型及名称下拉框")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('production:seedre:croptype')")
    @GetMapping("/croptype")
    public TableDataInfo operation()
    {
        //startPage();
        List<SeedReturn> list = seedReturnService.getCropType();
        List<SimpleProduction> simpleProductions = new ArrayList<>();
        for (SeedReturn seedReturn:list) {
            Long id = seedReturn.getCropTypeId();
            String name = seedReturn.getCropType();
            SimpleProduction simpleProduction = new SimpleProduction(id,name);
            simpleProductions.add(simpleProduction);
        }
        return getDataTable(simpleProductions);
    }

    /**
     * 获取品种名称列表下拉框
     */
    @ApiOperation("获取品种名称列表下拉框")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('production:seedre:varietyname')")
    @GetMapping("/varietyname/{cropTypeId}")
    public TableDataInfo getVarietyName(@PathVariable("cropTypeId") Long cropTypeId)
    {
        List<String> list = seedReturnService.getVarietyName(cropTypeId);
        return getDataTable(list);
    }

    /**
     * 获取用户列表下拉框
     */
    @ApiOperation("获取用户列表下拉框")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('production:seedre:username')")
    @GetMapping("/username")
    public TableDataInfo getUserName(){

        List<SeedReturn> list = seedReturnService.getUserName();
        List<SimpleProduction> simpleProductions = new ArrayList<>();
        for (SeedReturn seedReturn:list) {
            Long id = seedReturn.getUserId();
            String name = seedReturn.getUserName();
            SimpleProduction simpleProduction = new SimpleProduction(id,name);
            simpleProductions.add(simpleProduction);
        }
        return getDataTable(simpleProductions);
    }

    /**
     * 获取公司列表下拉框
     */
    @ApiOperation("获取公司列表下拉框")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('production:seedre:origincompany')")
    @GetMapping("/origincompany/{cropTypeId}")
    public TableDataInfo getOriginCompany(@PathVariable("cropTypeId") Long cropTypeId)
    {
        List<String> list = seedReturnService.getOriginCompany(cropTypeId);
        return getDataTable(list);
    }
}
